package com.ruoyi.web.controller;


import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.file.FileTypeUtils;
import com.ruoyi.common.core.utils.file.MimeTypeUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;
import com.ruoyi.system.api.model.LoginUser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.PDFTextStripperByArea;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import technology.tabula.ObjectExtractor;
import technology.tabula.Page;
import technology.tabula.Rectangle;
import technology.tabula.Table;
import technology.tabula.extractors.BasicExtractionAlgorithm;

import org.apache.pdfbox.pdmodel.PDDocument;

import java.io.File;
import java.io.IOException;
import java.util.List;

import java.util.Arrays;

import static com.ruoyi.common.core.web.domain.AjaxResult.error;

/**
 * 解析上传pdf
 */

@RestController
@RequestMapping("/templateHandle")
public class HandleTemplatController {
    @Autowired
    private RemoteFileService remoteFileService;


    @PostMapping("/handle")
    public AjaxResult avatar(@RequestParam("file") MultipartFile file)
    {
        if (!file.isEmpty())
        {
            try (PDDocument document = PDDocument.load(new File("E:\\work\\超声波检测原始记录.pdf"))) {
                // 解析PDF文件
                PDFTextStripper pdfStripper = new PDFTextStripper();
                String text = pdfStripper.getText(document);
                System.out.println(text);
                PDFTextStripperByArea stripper = new PDFTextStripperByArea();
                stripper.setSortByPosition(true);

                // 定义表格区域
                Rectangle rect = new Rectangle(0, 200, 595, 842); // A4纸张大小
                stripper.addRegion("table", rect);

                stripper.extractRegions(document.getPage(0));
                String tableText = stripper.getTextForRegion("table");
                System.out.println(tableText);
            } catch (IOException e) {
                e.printStackTrace();
            }

            try (PDDocument document = PDDocument.load(new File("E:\\work\\超声波检测原始记录.pdf"))) {
                PDFTextStripper pdfStripper = new PDFTextStripper();
                String text = pdfStripper.getText(document);

                // 分割文本为行
                String[] lines = text.split("");
                for (String line : lines) {
                    // 分割行中的单元格
                    String[] cells = line.split("\\s+"); // 使用空格分割
                    for (String cell : cells) {
                        System.out.print(cell + "\t");
                    }
                    System.out.println();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }






            LoginUser loginUser = SecurityUtils.getLoginUser();
            String extension = FileTypeUtils.getExtension(file);
            if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.IMAGE_EXTENSION))
            {
                return error("文件格式不正确，请上传" + Arrays.toString(MimeTypeUtils.IMAGE_EXTENSION) + "格式");
            }
            R<SysFile> fileResult = remoteFileService.upload(file);
            if (StringUtils.isNull(fileResult) || StringUtils.isNull(fileResult.getData()))
            {
                return error("文件服务异常，请联系管理员");
            }
            String url = fileResult.getData().getUrl();

        }
        return error("上传图片异常，请联系管理员");
    }
}
